<script lang="ts">
export default {
  name: 'Category'
}
</script>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import GoodsItem from './components/GoodsItem.vue'
import { http } from '@/utils/request'
import type { TopCategory } from '@/types'

const route = useRoute()
const topCategory = ref<TopCategory>()
onMounted(async () => {
  const res = await http<TopCategory>('GET', '/category', { id: route.params.id })
  topCategory.value = res.data.result
})
</script>

<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <template v-if="topCategory">
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem>{{ topCategory?.name }}</XtxBreadItem>
        </template>
      </XtxBread>
      <!-- 所有二级分类 -->
      <div v-if="topCategory" class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in topCategory?.children" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <div v-else class="sub-list xtx-loading"></div>
      <!-- 楼层商品 -->
      <div v-if="topCategory" class="ref-goods">
        <template v-for="item in topCategory?.children" :key="item.id">
          <div class="head">
            <h3>- {{ item.name }} -</h3>
            <XtxMore />
          </div>
          <div class="body">
            <GoodsItem v-for="goods in item.goods" :key="goods.id" :goods-item="goods" />
          </div>
        </template>
      </div>
      <div v-else class="ref-goods xtx-loading"></div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.top-category {
  h3 {
    font-size: 28px;
    font-weight: normal;
    color: #666;
    text-align: center;
    line-height: 100px;
  }
}

.sub-list {
  // margin-top: 20px;
  min-height: 260px;
  background-color: #fff;

  &.xtx-loading {
    background: #fff url(@/assets/images/loading.gif) no-repeat center;
  }

  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 32px;

    li {
      width: 168px;
      height: 160px;

      a {
        display: block;
        font-size: 16px;
        text-align: center;

        img {
          width: 100px;
          height: 100px;
        }

        p {
          line-height: 40px;
        }

        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}

.ref-goods {
  position: relative;
  margin-top: 20px;
  min-height: 260px;
  background-color: #fff;

  &.xtx-loading {
    background: #fff url(@/assets/images/loading.gif) no-repeat center;
  }

  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }

    .tag {
      position: relative;
      top: -20px;
      color: #999;
      font-size: 20px;
      text-align: center;
    }
  }

  .body {
    display: flex;
    justify-content: space-around;
    padding: 0 40px 30px;
  }
}
</style>
